﻿using System;
using System.Reflection;
using ProgramMain.Framework;

namespace ProgramMain.OpticNet
{
    public partial class FrmVertexMap : ProgramMain.Framework.frmBase
    {
        public FrmVertexMap()
        {
            InitializeComponent();

            vertexMapCtl1.BeforeVertexJump += new EventHandler<IntEvent>(vertexMapCtl1_BeforeVertexJump);
            vertexMapDesignPanel1.BeforeVertexJump += new EventHandler<IntEvent>(vertexMapCtl1_BeforeVertexJump);
        }

        private int? VertexID
        {
            set
            {
                if (vertexMapCtl1.DetailID != value)
                {
                    vertexMapCtl1.SetZoom(10);
                    vertexMapCtl1.DetailID = value;
                    vertexMapDesignPanel1.DetailID = value;
                    UniqueIdent = GenerateFormIdent(value);
                }
            }
        }

        //   private event EventHandler ScaleChange;
        private static string GenerateFormIdent(int? VertexID)
        {
            if (!VertexID.HasValue)
                return GenerateUniqueIdent(MethodInfo.GetCurrentMethod().DeclaringType.Name);
            else
                return GenerateUniqueIdent(MethodInfo.GetCurrentMethod().DeclaringType.Name, VertexID.Value);
        }

        public static FrmVertexMap ShowForm(bool Refresh, int? VertexID)
        {
            string FormIdent = GenerateFormIdent(VertexID);
            FrmVertexMap frm = (FrmVertexMap)FindForm(FormIdent);
            if (frm != null)
            {
                frm.Activate();
                if (Refresh)
                    frm.vertexMapCtl1.RefreshControl(null);
            }
            else
            {
                frm = new FrmVertexMap();
                frm.VertexID = VertexID;
                frm.ShowAsMDI(Program.MainForm);
            }
            frm.vertexMapCtl1.CreateAutoConnect();
            return frm;
        }

        void vertexMapCtl1_BeforeVertexJump(object sender, IntEvent e)
        {
            vertexMapDesignPanel1.Zoom = 10;
            vertexMapCtl1.SetZoom(10);
            VertexID = e.ObjectID;
            vertexMapCtl1.Invalidate();
        }

        private void vertexMapDesignPanel1_ZoomChanged(object sender, EventArgs e)
        {
            vertexMapCtl1.SetZoom(vertexMapDesignPanel1.Zoom);
            vertexMapCtl1.ReDrawMap();
        }

        private void vertexMapDesignPanel1_VertexStateChanged(object sender, VertexMapDesignPanel.VertexStateEvent e)
        {
            vertexMapCtl1.VertexState = e.State;
        }

        private void vertexMapDesignPanel1_RefreshVertex(object sender, IntEvent e)
        {
            vertexMapCtl1.RefreshControl(e.ObjectID);
            
        }

        private void vertexMapCtl1_ZoomChanged(object sender, IntEvent e)
        {
            vertexMapDesignPanel1.Zoom += e.ObjectID.Value;
            vertexMapCtl1.SetZoom(vertexMapDesignPanel1.Zoom);
            vertexMapCtl1.ReDrawMap();
        }

        private void FrmVertexMap_BackColorChanged(object sender, EventArgs e)
        {
            vertexMapDesignPanel1.listVertex.BackColor = this.BackColor;
        }

        private void FrmVertexMap_Load(object sender, EventArgs e)
        {
            vertexMapDesignPanel1.listVertex.BackColor = this.BackColor;
        }

        private void vertexMapDesignPanel1_SendVertexCaption(object sender, StringEvent e)
        {
            if (!string.IsNullOrEmpty(e.Message))
            {
                Text = string.Format("Схема объекта [{0}]", e.Message);
                vertexMapCtl1.VertexCaption = Text;
                vertexMapCtl1.VertexAdres = vertexMapDesignPanel1.VertexAdres;
            }
        }

        private void vertexMapDesignPanel1_AllConnectionChanged(object sender, BoolEvent e)
        {
            vertexMapCtl1.AllConnection = e.Message; 
        }

        private void vertexMapDesignPanel1_Printing(object sender, EventArgs e)
        {
            vertexMapCtl1.Priting();
        }

        private void vertexMapDesignPanel1_CableBoxVisibleChanged(object sender, IntEvent e)
        {
            vertexMapCtl1.PlanType = e.ObjectID.Value;
        }

    }

    public enum VertexMapState { None, Connection }
}